﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using YmtAuth.Domain.Model.Safe;
using YmtAuth.Domain.Repository;

namespace YmtAuth.Repository.MySQL
{
    public class IpAreaTokenRepository : MySQLRepository<IpAreaTokenEntity>, IIpAreaTokenRepository
    {
        public IpAreaTokenRepository()
            : base(ConfigurationManager.AppSettings["UserIpAreaTokenAuthHistory_MySqlConnection"], "UserTokenAuthHistory")
        {

        }

        public IpAreaTokenEntity FindLastTokenAuth(int userid, string country, string province, string city)
        {
            var sql =
                "select `UserId`,`Country`,`Province`,`City`,`LastAuthTime` from UserTokenAuthHistory where `UserId`=@UserId AND `Country`=@Country AND `Province`=@Province AND `City`=@City order by LastAuthTime desc limit 1";
            var par =
                this.Parameters.AddInt("@UserId", userid)
                    .AddString("@Country", country)
                    .AddString("@Province", province)
                    .AddString("@City", city).Build();

            return Query(sql, par, 30, CommandType.Text).FirstOrDefault();
        }

//        public void InsertAsync(IpAreaTokenEntity entity)
//        {
//            InsertAsync(entity,true);
//        }
    }
}
